# coding=utf8
import os.path
import time

from myself_robot.driven.driven import Driver
from myself_robot.mytools.tools import *


class Login:
    d = Driver().get_driver()

    def __init__(self):
        self.d.get("http://36.133.205.34:28080/WoniuBoss4.0")

    def login(self, *args):

        self.d.refresh()
        self.d.implicitly_wait(10)

        # time.sleep(2)
        self.d.find_element_by_name("userName").send_keys(args[0])
        # time.sleep(2)
        self.d.find_element_by_name("userPass").send_keys(args[1])
        # time.sleep(2)
        self.d.find_element_by_name("checkcode").clear()
        self.d.find_element_by_name("checkcode").send_keys(args[2])
        # time.sleep(2)
        self.d.find_element_by_xpath("//*[@id='form-login']/div/div/div[2]/button").click()
        self.d.implicitly_wait(0)

        if Driver().is_element(self.d, '//p[contains(text(),"用户名不能为空")]'):
            return self.image_fail()
        elif Driver().is_element(self.d, '//p[contains(text(),"密码不能为空")]'):
            return self.image_fail()
        elif Driver().is_element(self.d, '//p[contains(text(),"用户名或密码错误")]'):
            return self.image_fail()
        elif Driver().is_element(self.d, '//p[contains(text(),"验证码不能为空")]'):
            return self.image_fail()
        else:
            self.d.implicitly_wait(10)
            if Driver().is_element1(driver=self.d, how="partial link text", what="注销"):
                return self.image_pass()

            else:
                return self.image_fail()

    def image_fail(self):
        imagefil = pro_path() + rf"/images/{time.time()}.png"
        self.d.save_screenshot(imagefil)
        imagefil = r"../" + imagefil[imagefil.find("images"):]
        return "login-fail", imagefil

    def image_pass(self):
        imagefil = pro_path() + rf"/images/{time.time()}.png"
        self.d.save_screenshot(imagefil)
        imagefil = r"../" + imagefil[imagefil.find("images"):]
        self.d.find_element_by_xpath("/html/body/div[2]/div[2]/ul/li[2]/a").click()
        return "login-pass", imagefil

        # time.sleep(1)
        # self.d.quit()


if __name__ == '__main__':
    login = Login()
    login.login()
